import { 
  LOGIN_USER_REQUEST, 
  LOGIN_USER_FAILURE, 
  LOGIN_USER_SUCCESS, 
  LOGOUT_USER, 
  CLEAR_ERROR 
} from '../constants';

const initialState = {
  token: null,
  isAuthenticated: false,
  isAuthenticating: false,
  statusText: null,
  openSnackbar: false,
  phone: null,
  userId: null
};

function func(state = initialState, action) {
  switch (action.type) {
    case LOGIN_USER_REQUEST:
      return Object.assign({}, state, {
        isAuthenticating: true,
        isAuthenticated: false,
        token: null,
        statusText: null,
        openSnackbar: false,
        phone: null,
        userId: null
      });
    case LOGIN_USER_FAILURE:
      return Object.assign({}, state, {
        isAuthenticating: false,
        isAuthenticated: false,
        token: null,
        statusText: action.payload.msg,
        openSnackbar: true,
        phone: null,
        userId: null
      });
    case LOGIN_USER_SUCCESS:
      return Object.assign({}, state, {
        isAuthenticating: false,
        isAuthenticated: true,
        token: action.payload.token,
        statusText: null,
        openSnackbar: false,
        phone: action.payload.phone,
        userId: action.payload.userId
      });
    case LOGOUT_USER:
      return Object.assign({}, state, {
        isAuthenticating: false,
        isAuthenticated: false,
        token: null,
        statusText: null,
        openSnackbar: false,
        phone: null,
        userId: null
      });
    case CLEAR_ERROR:
      return Object.assign({}, state, {
        statusText: null,
        openSnackbar: false
      });
    default:
      return state
  }
};

export default func;